<template>
  <div class="profile-page">
    <!-- 头部背景 -->
    <div class="profile-header">
      <Uploader :after-read="handleUpload"
        :before-delete="beforeImgUpload"
        class="avatar-uploader"
        :max-size="params.fileSize">
        <Image round fit="cover"
          class="user-avatar" :show-loading="loading"
          :src="userInfo.avatar">
          <template v-if="!userInfo.avatar">
            <Icon name="user-circle"
              class="default-avatar" />
          </template>
          <template #loading>
            <Loading type="spinner" size="20" />
          </template>
        </Image>
        <div class="avatar-mask">
          <Icon name="photograph" size="20" />
        </div>
      </Uploader>
    </div>

    <!-- 用户信息 -->
    <CellGroup class="info-group" inset>
      <Cell title="账号"
        :value="userInfo.userName" />
      <Cell title="手机号"
        :value="userInfo.phonenumber" />
      <Cell title="余额">
        <span class="balance">¥
          {{ userInfo.balance ? userInfo.balance.toFixed(2) : '0.00' }}</span>
      </Cell>
      <Cell title="昵称"
        :value="userInfo.nickName" />
      <Cell title="实名认证"
        :value="userInfo.realFlag === RealAuthType.已实名 ? '已实名' : '未实名'" />
      <Cell title="修改密码" icon="lock" is-link
        @click="$router.replace({ name: 'ChangePassword' })" />
    </CellGroup>
  </div>
</template>

<script setup lang="ts">
import { storeToRefs } from 'pinia';
import {
  Cell,
  CellGroup,
  Icon, Image, Loading, Uploader,
  type UploaderFileListItem,
} from 'vant';

import useUserStore from '@/stores/user.store';

import { RealAuthType } from '../const';
import useUploadFile from '../hooks/useUploadFile';

const userStore = useUserStore();
const { userInfo } = storeToRefs(userStore);

const {
  params, beforeImgUpload, handleUploadFile, handleUpdateAvatar, loading,
} = useUploadFile();

const handleUpload = async (items: UploaderFileListItem | UploaderFileListItem[]) => {
  params.value.file = (items as UploaderFileListItem).file!;
  params.value.fileName = (items as UploaderFileListItem).file!.name;
  await handleUploadFile();
  await handleUpdateAvatar();
  userStore.fetchUserInfo();
};
</script>

<style lang="less" scoped>
.profile-page {
  height: 100%;
  background: var(--van-background-2);
}

.profile-header {
  height: 200px;
  display: flex;
  justify-content: center;
  align-items: flex-end;
  padding-bottom: 30px;
  position: relative;
  background: linear-gradient(135deg, var(--van-primary-color) 30%, var(--van-primary-dark-color));

  &::after {
    content: '';
    position: absolute;
    bottom: -20px;
    left: 0;
    right: 0;
    height: 40px;
    background: var(--van-background-2);
    border-radius: 20px 20px 0 0;
  }
}

.avatar-uploader {
  position: relative;
  z-index: 1;

  .user-avatar {
    width: 100px;
    height: 100px;
    border: 3px solid #fff;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
  }

  .default-avatar {
    font-size: 100px;
    color: var(--van-gray-5);
  }

  .avatar-mask {
    position: absolute;
    bottom: -8px;
    right: -8px;
    width: 32px;
    height: 32px;
    background: var(--van-primary-color);
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    color: #fff;
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
  }
}

.info-group {
  margin-top: 40px;

  .balance {
    color: var(--van-primary-color);
    font-size: 16px;
    font-weight: bold;
  }
}

.edit-group {
  margin-top: 16px;
}

// 暗黑模式适配
.van-theme-dark {
  .profile-header {
    &::after {
      background: var(--van-gray-8);
    }
  }

  .user-avatar {
    border-color: var(--van-gray-8);
  }
}
</style>
